package com.probiz.estore.order.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.common.model.order.OrderAudit;
import com.probiz.estore.common.model.order.OrderPayment;
import com.probiz.estore.common.model.order.OrderPick;
import com.probiz.estore.common.model.order.OrderShipment;
import com.probiz.estore.common.model.order.SalesOrder;
import com.probiz.estore.common.util.NumberUtil;
import com.probiz.estore.core.util.ContextUtil;
import com.probiz.estore.order.OrderConstants;
import com.probiz.estore.order.dao.OrderAuditDao;
import com.probiz.estore.order.model.OrderCancelModel;
import com.probiz.estore.order.model.OrderCompleteModel;
import com.probiz.estore.order.service.OrderAuditHelper;
import com.probiz.estore.webapp.util.RequestContext;

/**
 * 订单日志审计工具类
 * 
 * 对订单主要数据的变化和流程操作进行记录
 * 
 */
public class OrderAuditHelperImpl implements OrderAuditHelper {
	private OrderAuditDao orderAuditDao;
	public void setOrderAuditDao(OrderAuditDao orderAuditDao) {
		this.orderAuditDao = orderAuditDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogCreateOrderByCustomer(com.probiz.estore.common.model.order.SalesOrder)
	 */
	public void doLogCreateOrderByCustomer(SalesOrder salesOrder) {
		createOrderAudit(salesOrder.getSalesOrderId(), getMessage("salesOrder.log.process.createOrderByCustomer", NumberUtil.format2String(salesOrder.getTotalAmount())), OrderConstants.TRANSACTION_TYPE_CREATE_ORDER_BYCUSTOMER);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogCreateOrderByAdmin(com.probiz.estore.common.model.order.SalesOrder)
	 */
	public void doLogCreateOrderByAdmin(SalesOrder salesOrder) {
		createOrderAudit(salesOrder.getSalesOrderId(), getMessage("salesOrder.log.process.createOrderByAdmin", NumberUtil.format2String(salesOrder.getTotalAmount())), OrderConstants.TRANSACTION_TYPE_CREATE_ORDER_BYADMIN);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogConfirmOrder(com.probiz.estore.common.model.order.SalesOrder)
	 */
	public void doLogConfirmOrder(SalesOrder salesOrder) {
		createOrderAudit(salesOrder.getSalesOrderId(), getMessage("salesOrder.log.process.confirmOrder"), OrderConstants.TRANSACTION_TYPE_CONFIRM_ORDER);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogPayOrder(com.probiz.estore.common.model.order.SalesOrder,
	 *      com.probiz.estore.common.model.order.OrderPayment)
	 */
	public void doLogPayOrder(SalesOrder salesOrder, OrderPayment orderPayment) {
		createOrderAudit(salesOrder.getSalesOrderId(), getMessage("salesOrder.log.process.payOrder", NumberUtil.format2String(orderPayment.getPaymentAmount()), orderPayment.getPaymentTypeDesc(), orderPayment.getDocumentNo()), OrderConstants.TRANSACTION_TYPE_PAY_ORDER);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogRefundOrder(com.probiz.estore.common.model.order.SalesOrder,
	 *      com.probiz.estore.common.model.order.OrderPayment)
	 */
	public void doLogRefundOrder(SalesOrder salesOrder, OrderPayment orderPayment) {
		createOrderAudit(salesOrder.getSalesOrderId(), getMessage("salesOrder.log.process.refundOrder", NumberUtil.format2String(orderPayment.getPaymentAmount()), orderPayment.getPaymentTypeDesc(), orderPayment.getDocumentNo()), OrderConstants.TRANSACTION_TYPE_REFUND_ORDER);
	};

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogShipOrder(com.probiz.estore.common.model.order.SalesOrder,
	 *      com.probiz.estore.common.model.order.OrderShipment)
	 */
	public void doLogShipOrder(SalesOrder salesOrder, OrderShipment orderShipment) {
		createOrderAudit(salesOrder.getSalesOrderId(), getMessage("salesOrder.log.process.shipOrder", orderShipment.getDeliveryTypeDesc(), orderShipment.getDocumentNo()), OrderConstants.TRANSACTION_TYPE_SHIP_ORDER);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogReturnOrder(com.probiz.estore.common.model.order.SalesOrder,
	 *      com.probiz.estore.common.model.order.OrderShipment)
	 */
	public void doLogReturnOrder(SalesOrder salesOrder, OrderShipment orderShipment) {
		createOrderAudit(salesOrder.getSalesOrderId(), getMessage("salesOrder.log.process.returnOrder", orderShipment.getDeliveryTypeDesc(), orderShipment.getDocumentNo()), OrderConstants.TRANSACTION_TYPE_RETURN_ORDER);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogEditOrder(com.probiz.estore.common.model.order.SalesOrder)
	 */
	public void doLogEditOrder(SalesOrder salesOrder) {
		createOrderAudit(salesOrder.getSalesOrderId(), getMessage("salesOrder.log.process.editOrder",NumberUtil.format2String(salesOrder.getTotalAmount())), OrderConstants.TRANSACTION_TYPE_EDIT_ORDER);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogPickOrder(com.probiz.estore.common.model.order.SalesOrder, com.probiz.estore.common.model.order.OrderPick)
	 */
	public void doLogPickOrder(SalesOrder salesOrder,OrderPick orderPick){
		createOrderAudit(salesOrder.getSalesOrderId(), getMessage("salesOrder.log.process.pickOrder",orderPick.getDocumentNo()), OrderConstants.TRANSACTION_TYPE_PICK_ORDER);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogCancelOrderByCustomer(com.probiz.estore.common.model.order.SalesOrder, com.probiz.estore.order.model.OrderCancelModel)
	 */
	public void doLogCancelOrderByCustomer(SalesOrder salesOrder,OrderCancelModel cancelModel){
		StringBuffer buffer = new StringBuffer();
		buffer.append(getMessage("salesOrder.log.process.cancelOrderByCustomer",salesOrder.getCancelReason()));
		//----------记录退还使用的优惠券---------------
		if(cancelModel.isUsedCouponNoReturnCommit()){
			buffer.append(getMessage("salesOrder.log.process.cancelOrder.returnUsedCouponNo",salesOrder.getUsedCouponNo()));
		}
		//----------记录退还支付的积分---------------
		if(cancelModel.isShopPointReturnCommit()){
			//返还支付积分
			buffer.append(getMessage("salesOrder.log.process.cancelOrder.returnShopPoint",salesOrder.getUsedShopPoint()));
		}
		
		//----------记录退还支付的礼券金额---------------
		if(cancelModel.getGiftCertificateNoList().size() > 0){
			for(int i=0; i< cancelModel.getGiftCertificateNoList().size(); i++){
				String giftCertificateNo = cancelModel.getGiftCertificateNoList().get(i);
				BigDecimal giftCertificateMoney = cancelModel.getGiftCertificateMoneyList().get(i);
				buffer.append(getMessage("salesOrder.log.process.cancelOrder.returnGiftCertificate",giftCertificateNo,NumberUtil.format2String(giftCertificateMoney)));
			}
		}
		createOrderAudit(salesOrder.getSalesOrderId(), buffer.toString(), OrderConstants.TRANSACTION_TYPE_CANCEL_ORDER_BYCUSTOMER);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogCancelOrderByAdmin(com.probiz.estore.common.model.order.SalesOrder, com.probiz.estore.order.model.OrderCancelModel)
	 */
	public void doLogCancelOrderByAdmin(SalesOrder salesOrder,OrderCancelModel cancelModel){
		StringBuffer buffer = new StringBuffer();
		buffer.append(getMessage("salesOrder.log.process.cancelOrderByAdmin",salesOrder.getCancelReason()));
		//----------记录退还使用的优惠券---------------
		if(cancelModel.isUsedCouponNoReturnCommit()){
			buffer.append(getMessage("salesOrder.log.process.cancelOrder.returnUsedCouponNo",salesOrder.getUsedCouponNo()));
		}
		//----------记录退还支付的积分---------------
		if(cancelModel.isShopPointReturnCommit()){
			buffer.append(getMessage("salesOrder.log.process.cancelOrder.returnShopPoint",cancelModel.getShopPoint()));
		}
		
		//----------记录退还支付的礼券金额---------------
		if(cancelModel.getGiftCertificateNoList().size() > 0){
			for(int i=0; i< cancelModel.getGiftCertificateNoList().size(); i++){
				String giftCertificateNo = cancelModel.getGiftCertificateNoList().get(i);
				BigDecimal giftCertificateMoney = cancelModel.getGiftCertificateMoneyList().get(i);
				buffer.append(getMessage("salesOrder.log.process.cancelOrder.returnGiftCertificate",giftCertificateNo,NumberUtil.format2String(giftCertificateMoney)));
			}
		}
		createOrderAudit(salesOrder.getSalesOrderId(), buffer.toString(), OrderConstants.TRANSACTION_TYPE_CANCEL_ORDER_BYADMIN);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogCancelOrder4NotPaid(com.probiz.estore.common.model.order.SalesOrder, com.probiz.estore.order.model.OrderCancelModel)
	 */
	public void doLogCancelOrder4NotPaid(SalesOrder salesOrder,OrderCancelModel cancelModel) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(getMessage("salesOrder.log.process.cancelOrder4NotPaid",salesOrder.getCancelReason()));
		//----------记录退还使用的优惠券---------------
		if(cancelModel.isShopPointReturnCommit()){
			buffer.append(getMessage("salesOrder.log.process.cancelOrder.returnUsedCouponNo",salesOrder.getUsedCouponNo()));
		}
		//----------记录退还支付的积分---------------
		if(cancelModel.isShopPointReturnCommit()){
			//返还支付积分
			buffer.append(getMessage("salesOrder.log.process.cancelOrder.returnShopPoint",salesOrder.getUsedShopPoint()));
		}
		
		//----------记录退还支付的礼券金额---------------
		if(cancelModel.getGiftCertificateNoList().size() > 0){
			for(int i=0; i< cancelModel.getGiftCertificateNoList().size(); i++){
				String giftCertificateNo = cancelModel.getGiftCertificateNoList().get(i);
				BigDecimal giftCertificateMoney = cancelModel.getGiftCertificateMoneyList().get(i);
				buffer.append(getMessage("salesOrder.log.process.cancelOrder.returnGiftCertificate",giftCertificateNo,NumberUtil.format2String(giftCertificateMoney)));
			}
		}
		createOrderAudit(salesOrder.getSalesOrderId(), buffer.toString(), OrderConstants.TRANSACTION_TYPE_CANCEL_ORDER_4NotPaid);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.order.service.OrderAuditHelper#doLogCompleteOrder(com.probiz.estore.common.model.order.SalesOrder, com.probiz.estore.order.model.OrderCompleteModel)
	 */
	public void doLogCompleteOrder(SalesOrder salesOrder,OrderCompleteModel completeModel){
		StringBuffer buffer = new StringBuffer();
		buffer.append(getMessage("salesOrder.log.process.completeOrder"));
		//----------赠送优惠券---------------
		if(completeModel.isCouponSendCommit()){
			buffer.append(getMessage("salesOrder.log.process.completeOrder.sendCoupon",salesOrder.getGainedCouponType().getName()));
		}
		//----------赠送积分---------------
		if(completeModel.isShopPointSendCommit()){
			buffer.append(getMessage("salesOrder.log.process.completeOrder.sendShopPoint",completeModel.getShopPoint()));
		}
		createOrderAudit(salesOrder.getSalesOrderId(), buffer.toString(), OrderConstants.TRANSACTION_TYPE_COMPLETE_ORDER);
	}

	/**
	 * 创建订单日志
	 * 
	 * @param salesOrderId
	 * @param detail
	 * @param transactionType
	 * @return
	 */
	private OrderAudit createOrderAudit(Integer salesOrderId, String detail, Short transactionType) {
		OrderAudit orderAudit = new OrderAudit();
		orderAudit.setTransactionType(transactionType);
		orderAudit.setCreateTime(new Date());
		orderAudit.setDetail(detail);
		orderAudit.setSalesOrderId(salesOrderId);
		orderAudit.setCreateBy(RequestContext.getCurrentUserId());
		orderAuditDao.save(orderAudit);
		return orderAudit;
	}

	private String getMessage(String key, Object... params) {
		return ContextUtil.getSpringContext().getMessage(key, params, null);
	}

}
